package com.sino.business.service;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;

import com.sino.bean.Basicinfo;
import com.sino.bean.Jjxyinfo;
import com.sino.bean.Ndjjxy;
import com.sino.business.IJjxyInfoService;
import com.sino.dao.IBasicInfoDAO;
import com.sino.model.JjxyInfoModel;

public class JjxyInfoServiceImp implements IJjxyInfoService {
	public final static  String ZB = "1";//总表
	public final static  String FB = "0";//分表
	private IBasicInfoDAO basicInfoDAO = null;
	/**
	 * Description       	 ：保存经济效益信息到数据库中
     * @param jjxyInfoModel	 ：经济效益信息基础类
     */
	public void saveJjxyInfo(JjxyInfoModel jjxyInfoModel){
		Jjxyinfo jjxyInfo = this.getJjxyinfo(jjxyInfoModel);
		boolean b = false;
		List temp = this.getBasicInfoDAO().findJjxyByZlIdClear(jjxyInfoModel.getZlId());
		Iterator it = temp.iterator();
		Jjxyinfo jjxyInfotemp;
		while(it.hasNext()){
			jjxyInfotemp = (Jjxyinfo)it.next();
			if(jjxyInfotemp.getBsw().equals(jjxyInfoModel.getBsw())){
				b = true;
			}
		}
		if(b){
			this.getBasicInfoDAO().updateJjxyInfo(jjxyInfo);
		}else{
			this.getBasicInfoDAO().saveJjxyInfo(jjxyInfo);
		}
	}
	/**
     * Description       	 ：更新经济效益信息到数据库中
     * @param jjxyInfoModel	 ：经济效益信息基础类
     */
	public void updateJjxyInfo(JjxyInfoModel jjxyInfoModel){}
	/**
     * Description       	   ：根据专利编号查找经济效益信息
     * @param zlId		 	   ：专利编号
     * @param bs 			   ：总表和分表标识
     */
	public JjxyInfoModel findJjxyByZlId(String zlId,final String bs){

		List temp = this.getBasicInfoDAO().findJjxyByZlId(zlId);
		Iterator it = temp.iterator();
		Jjxyinfo jjxyInfo;
		while(it.hasNext()){
			jjxyInfo = (Jjxyinfo)it.next();
			if(jjxyInfo.getBsw().equals(bs)){
				return this.getJjxyInfoModel(jjxyInfo);
			}
		}
		return new JjxyInfoModel();
	}
	
	//------------------------------------------------数据转换
	public JjxyInfoModel getJjxyInfoModel(Jjxyinfo jjxyInfo){
		JjxyInfoModel jjxyInfoModel = new JjxyInfoModel();
		Set ndJjxySet = jjxyInfo.getNdjjxies();
		
		jjxyInfoModel.setHsq(jjxyInfo.getHsq());
		jjxyInfoModel.setId(jjxyInfo.getId());
		jjxyInfoModel.setJsyj(jjxyInfo.getJsyj());
		jjxyInfoModel.setLxdh(jjxyInfo.getLxdh());
		jjxyInfoModel.setShxy(jjxyInfo.getShxy());
		jjxyInfoModel.setSsdw(jjxyInfo.getSsdw());
		jjxyInfoModel.setSsqk(jjxyInfo.getSsqk());
		jjxyInfoModel.setTbr(jjxyInfo.getTbr());
		jjxyInfoModel.setZlId(jjxyInfo.getBasicinfo().getZlid());
		jjxyInfoModel.setZtz(jjxyInfo.getZtz());

		Iterator ndJjxyIt = ndJjxySet.iterator();
		double xzxse4=0.0,xzlr4=0.0,xzss4=0.0,cswh4=0.0,jzje4=0.0;
		int i = 1;
		Ndjjxy ndJjxy;
		while(ndJjxyIt.hasNext()){
			ndJjxy = (Ndjjxy)ndJjxyIt.next();
			try{
				BeanUtils.copyProperty(jjxyInfoModel, "id"+i, ndJjxy.getId());
				BeanUtils.copyProperty(jjxyInfoModel, "nd"+i, ndJjxy.getNd());
				BeanUtils.copyProperty(jjxyInfoModel, "xzxse"+i, ndJjxy.getXzxse());
				BeanUtils.copyProperty(jjxyInfoModel, "xzlr"+i, ndJjxy.getXzlr());
				BeanUtils.copyProperty(jjxyInfoModel, "xzss"+i, ndJjxy.getXzss());
				BeanUtils.copyProperty(jjxyInfoModel, "cswh"+i, ndJjxy.getCswh());
				BeanUtils.copyProperty(jjxyInfoModel, "jzje"+i, ndJjxy.getJzje());
				
				if(ndJjxy.getXzxse()!=null)	xzxse4 = xzxse4 + ndJjxy.getXzxse();
				if(ndJjxy.getXzlr()!=null) xzlr4 = xzlr4 + ndJjxy.getXzlr();
				if(ndJjxy.getXzss()!=null) xzss4 = xzss4 + ndJjxy.getXzss();
				if(ndJjxy.getCswh()!=null) cswh4 = cswh4 + ndJjxy.getCswh();
				if(ndJjxy.getJzje()!=null) jzje4 = jzje4 + ndJjxy.getJzje();
				i++;
			}catch(Exception ex){
				ex.printStackTrace();
			}
			jjxyInfoModel.setXzlr4(xzlr4);
			jjxyInfoModel.setXzxse4(xzxse4);
			jjxyInfoModel.setXzss4(xzss4);
			jjxyInfoModel.setCswh4(cswh4);
			jjxyInfoModel.setJzje4(jzje4);
		}
		return jjxyInfoModel;
	}
	
	public Jjxyinfo getJjxyinfo(JjxyInfoModel jjxyInfoModel){
		Jjxyinfo jjxyinfo = new Jjxyinfo();
		Basicinfo basicinfo = new Basicinfo();
		
		Ndjjxy ndJjxy1 = new Ndjjxy();
		Ndjjxy ndJjxy2 = new Ndjjxy();
		Ndjjxy ndJjxy3 = new Ndjjxy();
		
		basicinfo.setZlid(jjxyInfoModel.getZlId());
		
		if(jjxyInfoModel.getId() != null){
			jjxyinfo.setId(jjxyInfoModel.getId());
		}
		jjxyinfo.setBasicinfo(basicinfo);
		jjxyinfo.setBsw(jjxyInfoModel.getBsw());
		jjxyinfo.setHsq(jjxyInfoModel.getHsq());
		jjxyinfo.setJsyj(jjxyInfoModel.getJsyj());
		jjxyinfo.setLxdh(jjxyInfoModel.getLxdh());
		jjxyinfo.setShxy(jjxyInfoModel.getShxy());
		jjxyinfo.setSsdw(jjxyInfoModel.getSsdw());
		jjxyinfo.setSsqk(jjxyInfoModel.getSsqk());
		jjxyinfo.setTbr(jjxyInfoModel.getTbr());
		jjxyinfo.setZtz(jjxyInfoModel.getZtz());
		
		
		ndJjxy1.setCswh(jjxyInfoModel.getCswh1());
		ndJjxy1.setId(jjxyInfoModel.getId1());
		ndJjxy1.setJzje(jjxyInfoModel.getJzje1());
		ndJjxy1.setNd(jjxyInfoModel.getNd1());
		ndJjxy1.setXzlr(jjxyInfoModel.getXzlr1());
		ndJjxy1.setXzss(jjxyInfoModel.getXzss1());
		ndJjxy1.setXzxse(jjxyInfoModel.getXzxse1());
		
		ndJjxy2.setCswh(jjxyInfoModel.getCswh2());
		ndJjxy2.setId(jjxyInfoModel.getId2());
		ndJjxy2.setJzje(jjxyInfoModel.getJzje2());
		ndJjxy2.setNd(jjxyInfoModel.getNd2());
		ndJjxy2.setXzlr(jjxyInfoModel.getXzlr2());
		ndJjxy2.setXzss(jjxyInfoModel.getXzss2());
		ndJjxy2.setXzxse(jjxyInfoModel.getXzxse2());
		
		ndJjxy3.setCswh(jjxyInfoModel.getCswh3());
		ndJjxy3.setId(jjxyInfoModel.getId3());
		ndJjxy3.setJzje(jjxyInfoModel.getJzje3());
		ndJjxy3.setNd(jjxyInfoModel.getNd3());
		ndJjxy3.setXzlr(jjxyInfoModel.getXzlr3());
		ndJjxy3.setXzss(jjxyInfoModel.getXzss3());
		ndJjxy3.setXzxse(jjxyInfoModel.getXzxse3());
		
		Set jjxySet = new HashSet();
		jjxySet.add(ndJjxy1);
		jjxySet.add(ndJjxy2);
		jjxySet.add(ndJjxy3);
		jjxyinfo.setNdjjxies(jjxySet);
		
		return jjxyinfo;
		
	}
	public IBasicInfoDAO getBasicInfoDAO() {
		return basicInfoDAO;
	}
	public void setBasicInfoDAO(IBasicInfoDAO basicInfoDAO) {
		this.basicInfoDAO = basicInfoDAO;
	}
	
}
